﻿using Kernel.Model.Demo.Peasy;
using Kernel.Repository.Demo.Peasy;
using Kernel.Peasy.Demo;
using Peasy;
using System.Diagnostics;

namespace WebAPI.Areas.Demo.Controllers
{
    [ApiVersion("1.0")]
    public class PeasyController : DemoBaseController
    {

        public PeasyController()
        {

        }

        /// <summary>
        /// 获取人员信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("person"), MapToApiVersion("1.0")]
        public async Task<IActionResult> GetPersons_V1_0()
        {
            var dataProxy = ServiceHost.Current.GetRequestService<IDataProxy<Person, int>>();
            //var service = new PersonService(new PersonStubDataProxy());
            var service = new PersonService(dataProxy);
            var getResult = await service.GetAllCommand().ExecuteAsync();

            if (getResult.Success)
            {
                foreach (var person in getResult.Value)
                    Debug.WriteLine(person.Name);  // prints each person's name retrieved from PersonMockDataProxy.GetAll
            }

            var newPerson = new Person() { Name = "Erlich Bachman", City = "Madison", Age = 32 };

            //测试模型验证
            var vail = newPerson.Validate();
            vail = newPerson.AdvancedValidate();

            var insertResult = await service.InsertCommand(newPerson).ExecuteAsync();

            if (insertResult.Success)
            {
                Debug.WriteLine(insertResult.Value.ID.ToString()); // prints the id value assigned via PersonMockDataProxy.Insert
            }

            return Ok(insertResult.Success);
        }
    }
}
